package com.jyet.jdbtool.connection.panel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import com.jyet.jdbtool.connection.entity.ConnectionConfig;

public class ConnectionTree extends JTree {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public ConnectionTree() {
        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("软件部");
        node1.add(new DefaultMutableTreeNode(new ConnectionConfig("小花")));
        node1.add(new DefaultMutableTreeNode(new ConnectionConfig("小虎")));
        node1.add(new DefaultMutableTreeNode(new ConnectionConfig("小龙")));

        DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("销售部");
        node2.add(new DefaultMutableTreeNode(new ConnectionConfig("小叶")));
        node2.add(new DefaultMutableTreeNode(new ConnectionConfig("小雯")));
        node2.add(new DefaultMutableTreeNode(new ConnectionConfig("小夏")));
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("职员管理");

        top.add(new DefaultMutableTreeNode(new ConnectionConfig("总经理")));
        top.add(node1);
        top.add(node2);
        this.addMouseMotionListener(new MouseMotionListener() {
            
            @Override
            public void mouseMoved(MouseEvent e) {
                // 什么都不用做
                
            }
            
            @Override
            public void mouseDragged(MouseEvent e) {
                // 什么都不用做
                
            }
        });
        this.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) getLastSelectedPathComponent();

                if (node == null) {
                    return;
                }

                Object object = node.getUserObject();
                if (node.isLeaf()) {
                    ConnectionConfig ConnectionConfig = (ConnectionConfig) object;
                    System.out.println("你选择了：" + ConnectionConfig.toString());
                }

            }
        });
    }

}
